package cn.hahah.module.blog.controller.admin.userfocus;

import cn.hahah.module.blog.dal.dataobject.users.UsersDO;
import cn.hahah.module.blog.service.users.UsersService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.CommonResult;
import cn.hahah.framework.common.util.object.BeanUtils;
import static cn.hahah.framework.common.pojo.CommonResult.success;

import cn.hahah.framework.excel.core.util.ExcelUtils;

import cn.hahah.framework.apilog.core.annotation.ApiAccessLog;
import static cn.hahah.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.hahah.module.blog.controller.admin.userfocus.vo.*;
import cn.hahah.module.blog.dal.dataobject.userfocus.UserFocusDO;
import cn.hahah.module.blog.service.userfocus.UserFocusService;

@Tag(name = "管理后台 - 用户关注")
@RestController
@RequestMapping("/blog/user-focus")
@Validated
public class UserFocusController {

    @Resource
    private UserFocusService userFocusService;
    @Resource
    private UsersService usersService;

    @PostMapping("/create")
    @Operation(summary = "创建用户关注")
    @PreAuthorize("@ss.hasPermission('blog:user-focus:create')")
    public CommonResult<Long> createUserFocus(@Valid @RequestBody UserFocusSaveReqVO createReqVO) {
        return success(userFocusService.createUserFocus(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户关注")
    @PreAuthorize("@ss.hasPermission('blog:user-focus:update')")
    public CommonResult<Boolean> updateUserFocus(@Valid @RequestBody UserFocusSaveReqVO updateReqVO) {
        userFocusService.updateUserFocus(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户关注")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:user-focus:delete')")
    public CommonResult<Boolean> deleteUserFocus(@RequestParam("id") Long id) {
        userFocusService.deleteUserFocus(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户关注")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:user-focus:query')")
    public CommonResult<UserFocusRespVO> getUserFocus(@RequestParam("id") Long id) {
        UserFocusDO userFocus = userFocusService.getUserFocus(id);
        return success(BeanUtils.toBean(userFocus, UserFocusRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户关注分页")
    @PreAuthorize("@ss.hasPermission('blog:user-focus:query')")
    public CommonResult<PageResult<UserFocusRespVO>> getUserFocusPage(@Valid UserFocusPageReqVO pageReqVO) {
        PageResult<UserFocusDO> pageResult = userFocusService.getUserFocusPage(pageReqVO);
        PageResult<UserFocusRespVO> result = BeanUtils.toBean(pageResult, UserFocusRespVO.class);
        for (UserFocusRespVO userFocusRespVO : result.getList()) {
            UsersDO usersDO = usersService.getUsers(userFocusRespVO.getUserId());
            UsersDO focusDo = usersService.getUsers(userFocusRespVO.getFocusId());

            if( usersDO != null){
                userFocusRespVO.setUserName(usersDO.getUsername());
            }
            if( focusDo != null){
                userFocusRespVO.setFocusName(focusDo.getUsername());
            }
        }

        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户关注 Excel")
    @PreAuthorize("@ss.hasPermission('blog:user-focus:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserFocusExcel(@Valid UserFocusPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserFocusDO> list = userFocusService.getUserFocusPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户关注.xls", "数据", UserFocusRespVO.class,
                        BeanUtils.toBean(list, UserFocusRespVO.class));
    }

}